﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SimpleSM
{
    public partial class SessionType : Form
    {
        private int session_id = 0;
        private int id = 0;

        public SessionType(int session_id, int id = 0)
        {
            InitializeComponent();
            this.id = id;
            this.session_id = session_id;
        }

        private void SessionType_Load(object sender, EventArgs e)
        {
            DataTable dt = new TSession().Get();
            cbTypeSession.DataSource = dt;
            if (this.id != 0)
            {
                DataTable dt0 = new SessionT().Get(this.id);
                cbTypeSession.SelectedValue = dt0.Rows[0][2];
                tbPort.Text = dt0.Rows[0][3].ToString();
                tbUser.Text = dt0.Rows[0][4].ToString();
                tbPassword.Text = dt0.Rows[0][5].ToString();
                tbArg1.Text = dt0.Rows[0][6].ToString();
                tbArg2.Text = dt0.Rows[0][7].ToString();
                cbTypeSession.Enabled = false;
            }
        }

        private void cbTypeSession_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbTypeSession.SelectedValue.ToString() != "")
            {
                DataTable dt = new TSession().Get(int.Parse(cbTypeSession.SelectedValue.ToString()));
                tbPort.Text = dt.Rows[0][4].ToString();
            }
        }

        private void tbPort_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !Char.IsNumber(e.KeyChar) && e.KeyChar != 8;
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            if (cbTypeSession.SelectedValue.ToString() == "")
            {
                MessageBox.Show("You must select type session", "Stop", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }
            if (this.id == 0)
            {
                new SessionT().Add(this.session_id, int.Parse(cbTypeSession.SelectedValue.ToString()), int.Parse(tbPort.Text), tbUser.Text, tbPassword.Text, tbArg1.Text, tbArg2.Text);
            }
            else
            {
                new SessionT().Update(this.id, int.Parse(tbPort.Text), tbUser.Text, tbPassword.Text, tbArg1.Text, tbArg2.Text);
            }
            this.Close();
        }
    }
}
